# -*- coding: utf-8 –*-
import datetime
import json

from mongoengine import (
    StringField, IntField, BooleanField,
    DateTimeField, ListField, PointField,
    ReferenceField, FloatField, Document,
    URLField, Q
)

from .mtu import Mtu


class Locality(Document):
    """
    国家
    """
    meta = {
        'collection': 'vacation_locality',
    }

    site_id = IntField()  # 城市或者国家，用途，聚合内容, 应为target_id
    site_type = IntField()
    name = StringField(max_length=50, required=True)
    name_en = StringField(max_length=50)
    time_zone = IntField()  # 正数为东区，负数为西区
    cover = URLField()
    location = PointField()
    itinerary_covers = StringField()
    published = BooleanField(default=False)
    position = IntField(default=0)


class Tag(Document):
    """
    标签 (用于攻略/目的地玩法等）
    """
    meta = {
        'collection': 'vacation_tag',
    }

    destination_infos = ListField(ReferenceField('DestinationInfo'))
    name = StringField(max_length=50, unique=True)
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)

    def __unicode__(self):
        return self.name

    def get_info(self):
        return {
            'id': self.pk,
            'name': self.name,
        }


class DestinationInfo(Document):
    """
    目的地详情
    """
    meta = {
        'collection': 'vacation_destinationinfo',
    }

    tags = ListField(ReferenceField('Tag'))
    destination = ReferenceField('Destination')
    max_days = IntField(null=True)
    min_days = IntField(null=True)
    percentage = FloatField(null=True)
    periphery = StringField(null=True)
    images = StringField()
    is_active = BooleanField(default=True)
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)


class Destination(Document):
    """
    目的地
    """
    meta = {
        'collection': 'vacation_destination',
    }

    site_id = IntField()  # 城市或者国家，用途，聚合内容, 应为target_id
    site_type = IntField()
    locality = ReferenceField(Locality)
    name = StringField(max_length=50)
    name_en = StringField(max_length=50)
    is_departured = BooleanField(default=False)  # 是否是出发目的地
    published = BooleanField(default=False)
    location = PointField()
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)

    def __unicode__(self):
        return self.name

    def get_info(self, more_info=False):
        # place = self.site.get_target() if self.site else None
        # if not place:
        #     return {}
        # info = place.get_info()
        def get_site_info():
            # 北京提供接口
            pass

        info = get_site_info()

        info['name'] = self.name
        result = {
            'destination_id': self.pk,
            'place': info,
            'name': self.name,
            'is_departured': self.is_departured,
            'is_airport': self.is_airport,
        }

        # 获取详细信息
        if more_info:
            condition = Q(destination=self.id)
            destination_info = DestinationInfo.objects.filter(condition).first()
            if destination_info:
                # destination_info = self.destinationinfo
                result['max_days'] = destination_info.max_days
                result['min_days'] = destination_info.min_days
                result['periphery'] = destination_info.periphery.split('、')
                destination_images = destination_info.images
                if destination_images:
                    destination_images = json.loads(destination_images)
                result['images'] = destination_images
                tags = destination_info.tags
                result['tags'] = tags.values_list('name', flat=True)
                result['recommend_days'] = destination_info.min_days

        return result


class DestinationOption(Document):
    """
    目的地玩法
    """
    meta = {
        'collection': 'vacation_destinationoption',
    }

    name = StringField(max_length=50)
    description = StringField()
    destination = ReferenceField(Destination)
    images = StringField()
    is_active = BooleanField(default=True)
    is_booked = BooleanField(default=False)  # 是否是预定线路数据
    # 预订数据使用  在整个行程中第一天
    day = StringField(max_length=50)
    user = IntField(null=True)
    published = BooleanField(default=False)
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)


class OptionMtu(Document):
    """
    目的地玩法的mtu
    """
    meta = {
        'collection': 'vacation_optionmtu',
    }

    mtu = ReferenceField(Mtu)
    option = ReferenceField(DestinationOption)
    section = IntField()  # 0: am, 1: pm, 2: night
    cover = StringField(max_length=100)
    is_active = BooleanField(default=True)
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)
